﻿using AutoMapper;
using FlyingHat.Core.Entities;
using FlyingHat.Model.Models;
using FlyingHat.Model.Models.Global;
using FlyingHat.Repository.IRepository;
using FlyingHat.Service.IService;

namespace FlyingHat.Service.Service
{
    public class BookService : IBookService
    {
        private readonly IBookRepository bookRepository;
        private readonly IMapper mapper;
        public BookService(IBookRepository bookRepository, IMapper mapper)
        {
            this.bookRepository = bookRepository;
            this.mapper = mapper;
        }

        public async Task<IReturnModel> AddOrUpdate(BookKeyModel model)
        {
            if (model.Id == 0)
            {
                var entity = mapper.Map<Book>(model);
                if (!string.IsNullOrWhiteSpace(model.ISBN))
                {
                    // 生成条形码
                    entity.BarCode = "";
                }
                var row = await bookRepository.AddAsync(entity);
                return ReturnModel.Result(row > 0, "保存");
            }
            else
            {
                var book = await bookRepository.FirstOrDefaultAsync(model.Id);
                if (book != null)
                {
                    book.Name = model.Name;
                    book.Author = model.Author;
                    book.Cover = model.Cover;
                    if (book.ISBN != model.ISBN)
                    {
                        book.ISBN = model.ISBN;
                        if (!string.IsNullOrWhiteSpace(model.ISBN))
                        {
                            // 生成条形码
                            book.BarCode = "";
                        }
                    }
                    book.Press = model.Press;
                    book.Pdf = model.Pdf;
                    var flag = await bookRepository.UpdateAsync(book);
                    return ReturnModel.Result(flag, "保存");
                }
                else
                {
                    return ReturnModel.Fail("图书不存在");
                }
            }
        }

        public async Task<IReturnModel> Delete(long id)
        {
            // TODO
            // 需检测是否被借出
            var flag = await bookRepository.DeleteAsync(id);
            return ReturnModel.Result(flag, "删除");
        }

        public async Task<IReturnModel> GetList(string? name, string? author, long? libraryId, long? bookself, int page, int limit)
        {
            var list = await bookRepository.GetAllAsync();

            if (!string.IsNullOrWhiteSpace(name))
            {
                list = list.Where(i => i.Name.Contains(name));
            }
            if (!string.IsNullOrWhiteSpace(author))
            {
                list = list.Where(i => i.Author.Contains(author));
            }
            var count = list.Count();
            var res = list.OrderByDescending(i => i.CreatedTime).Skip((page - 1) * limit).Take(limit).ToList();
            var resList = mapper.Map<List<BookModel>>(res);
            return ReturnModel.Page(count, resList);
        }
    }
}
